﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Utils;

namespace UI.ModSalary
{
    public partial class JjProducts : System.Web.UI.Page
    {
        JjProdBiz jjProdBiz = new JjProdBiz();
        protected void Page_Load(object sender, EventArgs e)
        {
         
            if(!IsPostBack)
            {
                DataBond("", "Y");
            }
        }
        private void DataBond(string serWord,string  statu)
        {
            DataTable dt = jjProdBiz.GetLists(serWord, statu);
            gvList.DataSource = dt;
            gvList.DataBind();
        }
        protected void gvProducts_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "cmdClose":
                        string code = e.CommandArgument.ToString();
                        SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
                        sdList.Add("ProCode", code);
                        sdList.Add("Statu", "N");
                        if (jjProdBiz.Update(sdList))
                        {
                            DataBond("", "Y");
                            JSUtility.Alert(this, "修改成功！");
                        }
                        else
                            JSUtility.Alert(this, "修改失败！");
                        break;

                    case "cmdDelete":
                        string code2 = e.CommandArgument.ToString();
                        if (string.IsNullOrWhiteSpace(code2)) return;
                        if (jjProdBiz.Delete(code2))
                        {
                            DataBond("", "Y");
                            JSUtility.Alert(this, "删除成功！");
                        }
                        else
                            JSUtility.Alert(this, "删除失败！");

                        break;
                    default:

                        break;
                }
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message);
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string proCode = txtProCode.Text.Trim();
            string proName = txtName.Text.Trim();
            if (string.IsNullOrWhiteSpace(proCode) || string.IsNullOrWhiteSpace(proName))
            {
                JSUtility.Alert(this, "产品编号和名称不能为空！");
                return;
            }
            if (jjProdBiz.IsExist(proCode))
            {
                JSUtility.Alert(this, "产品编号已经被使用，请更换一个！");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ProCode", proCode);
            sdList.Add("Name", proName);
            sdList.Add("ProModel", txtProModel.Text.Trim());
            sdList.Add("TypeName", txtTypeName.Text);
            sdList.Add("Unit", txtUnit.Text.Trim());           
            if (jjProdBiz.Insert(sdList))
            {
                JSUtility.Alert(this, "增加成功！");
                DataBond("", "Y");
            }
            else
                JSUtility.Alert(this, "增加失败！");

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string proCode = txtProCode.Text.Trim();
            string proName = txtName.Text.Trim();
            if (string.IsNullOrWhiteSpace(proCode) || string.IsNullOrWhiteSpace(proName))
            {
                JSUtility.Alert(this, "产品编号和名称不能为空！");
                return;
            }

            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ProCode", proCode);
            sdList.Add("Name", proName);
            sdList.Add("ProModel", txtProModel.Text.Trim());
            sdList.Add("TypeName", txtTypeName.Text);
            sdList.Add("Unit", txtUnit.Text);           
            if (jjProdBiz.Update(sdList))
                JSUtility.Alert(this, "修改成功！");
            else
                JSUtility.Alert(this, "修改失败！");
        }

        protected void btnProSearch_Click(object sender, EventArgs e)
        {
            string statu = "Y";
            if (rbClose.Checked) statu = "N";          
           string serWord= txtSerWord.Text.Trim();
           DataBond(serWord, statu);
          
        }
    }
}